<!DOCTYPE html>
<style>
  iframe {
    height: 300px;
    width: 300px;
  }
</style>

<body>
  <iframe></iframe>
</body>

<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<script type="text/javascript">
  'use strict';
  var iframe = document.querySelector('iframe');
  var loadCount = 0;

  // The test does the following navigation steps for iframe
  // 1. load blank1
  // 2. load blank2
  // 3. go back to blank1 
  async_test(function(t) {
    iframe.src = './resources/blank1.html';

    iframe.onload = t.step_func(function() {
      loadCount += 1;
      switch (loadCount) {
        case 1:
          t.step(function() {
            assert_regexp_match(iframe.contentWindow.location.href, /blank1/, 'should be on first blank page');
            iframe.contentWindow.history.scrollRestoration = 'manual';
            assert_equals(iframe.contentWindow.history.scrollRestoration, 'manual');
            iframe.contentWindow.scrollTo(500, 500);
            assert_equals(iframe.contentWindow.scrollX, 500, 'scripted scrolling should take effect');
            assert_equals(iframe.contentWindow.scrollY, 500, 'scripted scrolling should take effect');

            // navigate to new page
            setTimeout(function() {
              iframe.src = './resources/blank2.html';
            }, 0);
          });
          break;
        case 2:
          t.step(function() {
            assert_regexp_match(iframe.contentWindow.location.href, /blank2/, 'should be on second blank page');
            assert_equals(iframe.contentWindow.history.scrollRestoration, 'auto', 'new page loads should set scrollRestoration to "auto"');

            setTimeout(function() {
              iframe.contentWindow.history.back();
            }, 0);
          });
          break;
        case 3:
          t.step(function() {
            // coming back from history, scrollRestoration should be restored to manual and respect
            assert_regexp_match(iframe.contentWindow.location.href, /blank1/, 'should be back on first blank page');
            assert_equals(iframe.contentWindow.history.scrollRestoration, 'manual', 'navigating back should retain scrollRestoration value');
            assert_equals(iframe.contentWindow.scrollX, 0, 'horizontal scroll offset should not be restored');
            assert_equals(iframe.contentWindow.scrollY, 0, 'vertical scroll offset should not be restored');
          });
          t.done();
          break;
        default:
          assert_unreached('iframe should load 3 times');
      }
    });
  }, 'Navigating to new page should reset to "auto"');
</script>